package contacts530.application;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author zhaoyana-
 */
final class Select extends AbstractTableModel{
     Vector title ,message ;
     PreparedStatement ps=null;
     Connection ct=null;
     ResultSet rs=null;
     public Select(){
         this.sql("select * from student");
     }
    public Select(String ss){
        this.sql(ss);
    }
    public void sql(String sql){       
        title = new Vector();
        title.add("姓名");  
        title.add("宿舍号");
        title.add("年龄"); 
        title.add("生日");
        title.add("手机");
        title.add("Email");
        title.add("QQ");
        message=new Vector();
        try{
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        String url = "jdbc:sqlserver://localhost;database =contactsdb;user=sa;password=zyn416427";
        ct = DriverManager.getConnection(url);
            ps=ct.prepareStatement(sql);
            rs=ps.executeQuery();
            
            while(rs.next()){
                Vector line= new Vector();
                    line.add(rs.getString(1));
                    line.add(rs.getString(2));
                    line.add(rs.getString(3));
                    line.add(rs.getString(4));
                    line.add(rs.getString(5));
                    line.add(rs.getString(6));
                    line.add(rs.getString(7));
                    message.add(line);                  
            }         
        }catch(ClassNotFoundException | SQLException e){
            System.out.println("Error "+e);
        }
        finally{
            try{
                if(rs!=null){
                    rs.close();
                }
                if(ps!=null){
                    ps.close();
                }
                if(ct!=null){
                    ct.close();
                }
            }catch(SQLException e){
                
            }
        }
    }


     @Override
        public int getRowCount() {
       return this.message.size();
         
    }

    @Override
    public int getColumnCount() {
     return this.title.size();
       // return 1;
    }

    @Override
    public Object getValueAt(int line, int lie) {
    	return ((Vector)this.message.get(line)).get(lie);
        
    }
     
     @Override
    public String getColumnName(int e){//
     return (String)this.title.get(e);
       // return "h";
    }


}
